package net;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class ChatServer {
    // 定义服务端的ServerSocket属性
    private ServerSocket serverSocket;
    private Socket socket;
    public ChatServer(){
        try {
            System.out.println("服务器启动");
            serverSocket = new ServerSocket(8080);
            System.out.println("服务器启动OK");

        }catch (IOException e){
            System.out.println("服务器启动失败");
            e.printStackTrace();
            System.exit(1);
        }

    }

    //启动服务读取数据
    public void start(){
        Scanner scanner =new Scanner(System.in);
        PrintWriter pw = null;
        BufferedReader br =null;
        try {
            boolean sw = true;
            while (true) {
                if(sw) {
                    socket = serverSocket.accept();
                    System.out.println("客户端连上了,Cline:" + socket);
                    br = new BufferedReader(
                            new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
                    pw = new PrintWriter(socket.getOutputStream(), true);
                }
                String len;
                if (sw) {
                    while (sw) {
                        if ("ok".equalsIgnoreCase((len = br.readLine()))) {
                            System.out.println("客户端" + socket.getPort() + "发送完毕...");
                            sw = false;
                            break;
                        } else {
                            if ("exit".equalsIgnoreCase(len)) {
                                System.out.println("聊天结束，关闭聊天室!");
                                return;
                            }
                            System.out.println("客户端" + socket.getPort() + "的信息为:" + len);
                        }
                    }
                }else {
                    System.out.println("输入ok结束发送，输入exit关闭聊天室");
                    while (!sw) {
                        System.out.print("回复客户"+socket.getPort()+"消息内容:");
                        String go = scanner.nextLine();
                        pw.println(go);

                        if (go.equalsIgnoreCase("ok")){
                            sw=true;
                            System.out.println("发送完毕，等待客户端发送...");
                            break;
                        }

                        if (go.equalsIgnoreCase("exit")) {
                            System.out.println("聊天结束，关闭聊天室!");
                            return;
                        }
                    }
                }

            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            scanner.close();
            if (pw!=null)pw.close();
            if(br!= null)try {
                br.close();
            }catch (IOException e){
                e.printStackTrace();
            }

            if (socket != null)try {
                socket.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args){
        new ChatServer().start();

    }

}
